#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the version 3 of the GNU Lesser General Public License
#   as published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright (c) NEC Deutschland GmbH, NEC HPC Europe
#
# $Id$
import time

__all__ = ["LogRecord"]


# Constants
NS_IN_S = 1000.0 * 1000.0 * 1000.0


class LogRecord(object):
    """
    LOG record object definition. A record contains:
        - the measurement time in nanoseconds
        - the state value (a string)
        - an output string associated to the state change.
    """
    def __init__( self, time_ns=0, value="", output="" ):
        self.value = value
        self.output = output
        if time_ns == 0:
            self.time_ns = long( time.time() ) * NS_IN_S
        else:
            self.time_ns = time_ns

    def __repr__(self):
        return "%s(%s, %s, %s)" % ( self.__class__.__name__, repr( self.time_ns ), repr( self.value ), repr( self.output ) )

    def __str__( self ):
        from time import gmtime
        t = gmtime( self.time_ns / NS_IN_S )
        t_str = "%02d.%02d.%d %02d:%02d:%02d" % (t.tm_mday, t.tm_mon, t.tm_year, \
                                                 t.tm_hour, t.tm_min, t.tm_sec)
        return self.__repr__() + "   # " + t_str

    def marshal( self, marshaller ):
        marshaller.writeUint64( "time_ns", self.time_ns )
        marshaller.writeString( "value", self.value )
        marshaller.writeString( "output", self.output )

    def unmarshal( self, unmarshaller ):
        self.time_ns = unmarshaller.readUint64( "time_ns" )
        self.value = unmarshaller.readString( "value" )
        self.output = unmarshaller.readString( "output" )

from basic_types.factory import Factory
Factory.set_factory( "LogRecord", LogRecord, __name__ )
